function getMenuTypeNameByCode(menuTypeCode) {
	if (menuTypeCode == 'top')
		return '<font color=red>顶部菜单</font>';
	else if (menuTypeCode == 'left')
		return '侧面菜单';
	else
		return '我也不知道['+menuTypeCode+']是什么菜单';
}
layui.config({
	base : '/js/layui-extends/'
}).use([ 'laytpl', 'treegrid', 'layer', 'form', 'layedit' ], function() {
	var laytpl = layui.laytpl,
		treegrid = layui.treegrid;
	treegrid.config.render = function(viewid, data) {
		var view = document.getElementById(viewid).innerHTML;
		return laytpl(view).render(data) || '';
	};



	var tree1 = treegrid.createNew({
		elem : 'menutree',
		view : 'view',
		url : '/admin/menu/get-menu.json',
		id : 'menuId',
		parentid : 'parentId',
		singleSelect : false
	});
	tree1.build();

	$('.layui-btn').on('click', function() {
		var id,
			row = tree1.getRow();
		if (row != null)
			id = row.id;

		switch ($(this).attr('lay-filter')) {
		case 'expandAll': {
			tree1.expandAll(id)
		}
		break;
		case 'collapseAll': {
			tree1.collapseAll(id)
		}
		break;
		case 'expand': {
			tree1.expand(id)
		}
		break;
		case 'collapse': {
			tree1.collapse(id)
		}
		break;
		}
	});
	//------------------------------------------------
	function openAddMenuWindow(optname) {
		var optstr = '新建菜单';
		if(optname == 'edit'){
			optstr = '编辑菜单';
		}
		var mylayer = layer.open({
			type : 1,
			title : optstr,
			skin : 'layui-layer-lan',
			anim : 5,
			shadeClose : false,
			//area : [ 'auto', '400px' ],
			offset: [($(window).height()/8), $(window).width()/4],
			content : '<div id="addMenuForm"></div>',
			btn : [ '提交', '取消' ],
			closeBtn : false,
			btn1 : function(index, layero) {
				$('#mybutton').click();
			},
			btn2 : function(index, layero) {
				layer.close(mylayer);
			}
		});
		var getTpl = formftl.innerHTML,
			view = document.getElementById('addMenuForm');
		laytpl(getTpl).render({}, function(html) {
			view.innerHTML = html;
		});
		var form = layui.form;
		form.render();
		if(optname == 'edit'){
			$('#optname').val('edit');
		}else{
			$('#optname').val('new');
		}
		form.verify({
			parentId : function(value) {
				if (value == '') {
					return '我的天！parentId呢？怎么不填？';
				}
			}
		});
		
		/**
		 * 表单验证通过后在这里提交表单
		 */
		form.on('submit(mybutton)', function(data) {
			if(optname == 'edit'){
				$.ajax({
				    type: 'POST',
				    url: '/admin/menu/updateMenuById.json',
				    data: data.field,
				    success: function (data) {
				        if(data.success){
				        	layer.msg(data.message);
				        	layer.close(mylayer);
				        	tree1.build();
				        }else{
				        	layer.msg(data.message,{icon: 5,anim: 6,time: 2000});
				        }
				    },
				    error: function(data) {
				        alert("error:"+data.responseText);
				    },
				    dataType: "json"
				})
			} else {
				$.ajax({
				    type: 'POST',
				    url: '/admin/menu/addMenu.json',
				    data: data.field,
				    success: function (data) {
				        if(data.success){
				        	layer.msg(data.message);
				        	layer.close(mylayer);
				        	tree1.build();
				        }else{
				        	layer.msg(data.message,{icon: 5,anim: 6,time: 2000});
				        }
				    },
				    error: function(data) {
				        alert("error:"+data.responseText);
				    },
				    dataType: "json"
				})
			}
			return false;
		});
	}
	//------------------------------------------------
	$('#addMenu').click(function() {
		openAddMenuWindow('new');
		var row = tree1.getRow();
		if(row != null){
			$('input[name="parentId"]').val(row.menuId);
		}
	});
	$('#refresh').click(function() {
		tree1.build();
	});
	$('#deleteMenu').click(function() {
		var row = tree1.getRow();
		if (row == null) {
			layer.msg('请选择删除记录', {
				icon : 5,
				anim : 6,
				time : 2000
			});
		} else {
			layer.confirm('删除菜单['+row.menuName+']么？', {icon: 3, skin : 'layui-layer-lan' ,title:'请确认'}, function(index){
				$.ajax({
				    type: 'POST',
				    url: '/admin/menu/deleteMenuById.json',
				    data:{'menuId':row.menuId,'parentId':row.parentId},
				    success: function (data) {
				        if(data.success){
				        	layer.msg(data.message);
				        	layer.close(index);
				        	tree1.build();
				        }else{
				        	layer.msg(data.message,{icon: 5,anim: 6,time: 2000});
				        }
				    },
				    error: function(data) {
				        alert("error:"+data.responseText);
				    },
				    dataType: "json"
				})
			});
			var form = layui.form;
			form.render();
		}
		
	});
	$('#editMenu').click(function() {
		var row = tree1.getRow();
		if (row == null) {
			layer.msg('请选择编辑记录', {
				icon : 5,
				anim : 6,
				time : 2000
			});
		} else {
			openAddMenuWindow('edit');
			$('input[name="menuName"]').val(row.menuName);
			$('input[name="menuUrl"]').val(row.menuUrl);
			$('input[name="menuId"]').val(row.menuId);
			$('input[name="parentId"]').val(row.parentId);
			$('input[name="sort"]').val(row.sort);
			$('input[name="menuType"]').each(function() {
				$(this).removeAttr("checked");
				if ($(this).attr('value') == row.menuType) {
					$(this).attr('checked', '');
				}
			});
			var form = layui.form;
			form.render();
		}
	})
});